<template>
  <el-container>
    <el-header style="height: 47px;">
      <p>设备信息列表</p>
    </el-header>
    <el-main>
      <el-row class="btn-group">
        <el-button class="btn" @click="handleCreateParkingMag"><span>增加</span><i class="el-icon-plus"></i>
        </el-button>
        <el-button class="btn" @click="handleUpdateParkingMag"><span>编辑</span><i class="el-icon-edit"></i>
        </el-button>
        <el-button class="btn" @click="handleDeleteParkingMag"><span>删除</span><i class="el-icon-delete"></i>
        </el-button>
<!--        <el-button class="btn" @click="handleImport"><span>导入</span><i class="el-icon-upload2"></i>
        </el-button>
        <el-button class="btn" @click="handleDownloadParkingMag"><span>导出</span><i class="el-icon-download"></i>
        </el-button>
        <el-button class="four-btn" @click="handleDownloadParkingMagExcel"><span>下载模板</span><i
          class="el-icon-download"></i>
        </el-button>-->
      </el-row>
      <el-table :data="dataList" v-loading="listLoading" element-loading-text="给我一点时间"
                highlight-current-row stripe
                style="width: 100%" ref="parkingMagTable" @selection-change="handleSelectChangeParkingMag">
        <el-table-column fixed type="selection" width="55"></el-table-column>
        <el-table-column fixed sortable align="center" label="设备编号" width="150" prop="equipment_no">
          <template slot-scope="scope">
            <span>{{scope.row.equipment_no}}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="设备位置" width="150" sortable prop="equipment_site">
          <template slot-scope="scope">
            <span>{{scope.row.equipment_site}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="设备类型" width="200" sortable prop="equipment_type">
          <template slot-scope="scope">
            <span>{{scope.row.equipment_type}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="设备型号" width="200" sortable prop="park_ssdwname">
          <template slot-scope="scope">
            <span>{{scope.row.equipment_model}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="管理单位" width="200" sortable prop="park_sfbz">
          <template slot-scope="scope">
            <span>{{scope.row.equipment_manager}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="维保单位" width="200" sortable prop="park_road">
          <template slot-scope="scope">
            <span>{{scope.row.equipment_operator}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="采购时间" width="200" sortable prop="park_dllxname">
          <template slot-scope="scope">
            <span>{{scope.row.equipment_buy_time}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="经度" width="200" sortable prop="park_long">
          <template slot-scope="scope">
            <span>{{scope.row.equipment_long}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="纬度" width="200" sortable prop="park_lati">
          <template slot-scope="scope">
            <span>{{scope.row.equipment_lati}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态" width="200" sortable prop="park_statusname">
          <template slot-scope="scope">
            <span>{{scope.row.equipment_status}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="备注" width="200" sortable prop="park_remark">
          <template slot-scope="scope">
            <span>{{scope.row.equipment_desc}}</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" align="center" width="100" label="操作">
          <template slot-scope="scope">
            <el-dropdown trigger="click" @command="handleCommand">
              <el-button class="btn-drop-down" @click="selectRow(scope.row)">
                <i class="el-icon-arrow-down"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="edit">编辑</el-dropdown-item>
                <el-dropdown-item command="del">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <el-footer>
      <el-pagination style="margin-top: 16px" background @size-change="handleSizeChangeParkingMag"
                     @current-change="handleCurrentChangeParkingMag"
                     :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-footer>
    <create-equipment-mag-dialog :dialogStatusParkMag="dialogStatusParkMag" :dialogVisible.sync="dialogCreateVisible"
                                 @createDataParkMag="createDataParkMag" :tempParkMag="tempParkMag"
                                 @updateDataParkMag="updateDataParkMag"></create-equipment-mag-dialog>
    <el-dialog :title="'导入'" :visible.sync="dialogUploadVisible" center>
      <el-upload
        class="upload-demo" ref="upload" action="https://jsonplaceholder.typicode.com/posts/"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :file-list="fileList"
        :auto-upload="false">
        <el-button slot="trigger" type="primary">选取文件</el-button>
        <el-button style="margin-left: 20px;" type="success" @click="submitUpload">上传到服务器</el-button>
        <div slot="tip" class="el-upload__tip">只能上传XXX文件，且不超过500kb</div>
      </el-upload>
    </el-dialog>
  </el-container>

</template>

<script>
  /* eslint-disable */
  import CreateEquipmentMagDialog from "./creatEquipmentMagDialog";
  import {createParkMag, updateParkMag, deleteParkMag} from "../../../api/parkingResource/parkingMag";
  import ElContainer from "element-ui/packages/container/src/main";
  import ElHeader from "element-ui/packages/header/src/main";
  import ElMain from "element-ui/packages/main/src/main";
  import ElFooter from "element-ui/packages/footer/src/main";

  export default {
    components: {
      ElFooter,
      ElMain,
      ElHeader,
      ElContainer,
      CreateEquipmentMagDialog,
    },
    name: "equipment-mag-table",
    props: ['searchList', 'listLoadingParkingMag'],
    watch: {
      searchList(val) {
        this.dataList = val.list
        this.total = val.count
      },
      listLoadingParkingMag(val) {
        this.listLoading = val
      }
    },
    data() {
      return {
        listLoading: false,
        downloadLoading: false,
        dialogCreateVisible: false,
        dialogUploadVisible: false,
        dialogStatusParkMag: '',
        dataList: [],
        selectList: [],
        fileList: [],
        total: 10,
        tempParkMag: {
          park_no: '',
          park_name: '',
          park_placeno: '',
          park_placeStart: '001',
          park_ssdw: '',
          park_ssdwname: '', park_sfbzname: '',
          park_sfbz: '', park_roadno: '', park_road: '', park_dllx: '',
          park_jdbsc: '', park_sroad: '', park_eroad: '', park_dlnc: '',
          park_sffs: '', park_jcfs: '', park_long: '', park_lati: '',
          park_sdate: '', park_edate: '', park_status: '1', park_remark: '',

          equipment_site: '',
          equipment_desc: '',
          equipment_status: '',
          equipment_type: '',
          equipment_person: '',
          equipment_model: '',
          equipment_manager: '',
          equipment_operator: '',
          equipment_buy_time: '',
          equipment_long: '',
          equipment_lati: '',
        },
        listQuery: {
          page: 1,
          limit: 20
        }
      }
    },
    methods: {
      handleCreateParkingMag() {
        this.resetTemp()
        this.dialogCreateVisible = true
        this.dialogStatusParkMag = 'create'
      },
      handleUpdateParkingMag() {
        // if(this.selectList.length!=1){
        //   this.$message({
        //     showClose: true,
        //     message: '请选择一条进行编辑',
        //     type: 'warning',
        //     duration: 2000
        //   });
        //   return
        // }
        this.dialogStatusParkMag = 'update'
        // this.tempParkMag = Object.assign({}, this.selectList[0]) // copy obj
        this.dialogCreateVisible = true
      },
      handleDeleteParkingMag() {
        this.deleteDataParkMag(this.selectList)
      },
      handleDownloadParkingMag() {
        if (this.selectList.length) {

        } else {
          this.$message({
            showClose: true,
            message: '至少选择一条',
            duration: 2000,
            type: 'warning'
          })
        }
        this.downloadLoading = true
      },
      handleDownloadParkingMagExcel() {
      },
      handleImport() {
        this.dialogUploadVisible = true
      },
      handleSelectChangeParkingMag(val) {
        this.selectList = val
      },
      handleSizeChangeParkingMag(val) {
        this.listQuery.limit = val
        this.$emit("handleSizeChangeParkingMag", val)
      },
      handleCurrentChangeParkingMag(val) {
        this.listQuery.page = val
        this.$emit("handleCurrentChangeParkingMag", val)
      },
      createDataParkMag(val) {
        createParkMag(val).then(res => {
          this.dataList.unshift(val)
          this.dialogCreateVisible = false
          this.$notify({
            title: '成功',
            message: '创建成功',
            type: 'success',
            duration: 2000
          })
        })
      },
      deleteDataParkMag(val) {
        if (val.length) {
          this.$confirm('此操作将永久删除选中的记录,是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            deleteParkMag(val).then(res => {
              for (const v of val) {
                let indexOf = this.dataList.indexOf(v);
                this.dataList.splice(indexOf, 1)
              }
              this.$message({
                showClose: true,
                message: '删除成功',
                type: 'success',
                duration: 2000
              });
            })
          })
        } else {
          this.$message({
            showClose: true,
            message: '请选择至少一条删除',
            type: 'warning',
            duration: 2000
          });
        }
      },
      updateDataParkMag(val) {
        updateParkMag(val).then(res => {
          this.dialogCreateVisible = false
          for (const v of this.dataList) {
            if (v.park_no === this.tempParkMag.park_no) {
              const index = this.dataList.indexOf(v)
              this.dataList.splice(index, 1, val)
              break
            }
          }
          this.$notify({
            title: '成功',
            message: '更新成功',
            type: 'success',
            duration: 2000
          })
        })
      },
      resetTemp() {
        this.tempParkMag = {
          park_no: '',
          park_name: '',
          park_placeno: '',
          park_placeStart: '001',
          park_ssdw: '',
          park_ssdwname: '', park_sfbzname: '',
          park_sfbz: '', park_roadno: '', park_road: '', park_dllx: '',
          park_jdbsc: '', park_sroad: '', park_eroad: '', park_dlnc: '',
          park_sffs: '', park_jcfs: '', park_long: '', park_lati: '',
          park_sdate: '', park_edate: '', park_status: '1', park_remark: ''
        }
      },
      submitUpload() {
        this.$refs.upload.submit();
      },
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      },
      handleCommand(val) {
        if (val == "edit") {
          this.handleUpdateParkingMag()
        } else if (val == "del") {
          let tempList = []
          tempList.push(this.tempParkMag)
          this.deleteDataParkMag(tempList)
        }
      },
      selectRow(val) {
        console.log(val)
        this.tempParkMag = Object.assign({}, val) // copy obj
      }
    }
  }
</script>

<style scoped>
  .btn-group > .el-button span {
    display: none;
  }

  .btn-group > .el-button:hover {
    background-color: #409EFF;
  }

  .btn-group > .el-button:hover span {
    display: block;
  }

  .btn-group > .el-button:hover i {
    display: none;
  }
</style>
